package com.yazi.mxz.person.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.GoodsGift;
import com.yazi.mxz.common.entity.beans.GoodsGiftId;
import com.yazi.mxz.common.entity.beans.GoodsPromote;
import com.yazi.mxz.common.entity.beans.GoodsPromoteId;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.enums.GoodsGiftType;
import com.yazi.mxz.common.entity.enums.GoodsPromoteStatus;
import com.yazi.mxz.common.entity.enums.GoodsPromoteType;
import com.yazi.mxz.common.service.GoodsGiftService;
import com.yazi.mxz.common.service.GoodsPromoteService;
import com.yazi.mxz.common.service.StoreGoodsService;
import com.yazi.mxz.person.controller.MyPageLink;

@Controller("shop-goodsPromoteController")
@RequestMapping("/shop/goodsPromote")
public class GoodsPromoteController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsPromoteController.class);
	
	@Resource
	StoreGoodsService storeGoodsService;
	@Resource
	GoodsPromoteService goodsPromoteService;
	@Resource
	GoodsGiftService goodsGiftService;
	
	@RequestMapping("/list")
	public String list(GoodsPromote goodsPromote,Searching si, Model model) {
		debug(log, "进入店铺商品促销列表页面");
		
		// 只查询当前店铺
		StoreGoods storeGoods = new StoreGoods();
		Store store = new Store();
		store.setStoreId(getStoreId());
		storeGoods.setStore(store);
		goodsPromote.setStoreGoods(storeGoods); 
		
		// 默认按促销开始时间倒序
		if (null == si.getSorts()) { 
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("startTime", true));
			si.setSorts(sorts);
		}
		Page<GoodsPromote> page = goodsPromoteService.list(goodsPromote, si);
		PageLink pl = new MyPageLink(page.getPaging());
		// 促销状态
		GoodsPromoteStatus[] gpStatuses = GoodsPromoteStatus.values();
		
		model.addAttribute("list", page.getList());
		model.addAttribute("pl", pl);
		model.addAttribute("gpStatuses", gpStatuses);
		model.addAttribute("goodsPromote", goodsPromote);
		model.addAttribute("searchWord", si.getSearchWord());
		
		return "shop/goodsPromote_list";
	}
	
	@RequestMapping("/addOrEdit")
	public String addOrEdit(long storeGoodsId, int type, HttpServletRequest request, Model model) {
		debug(log, "进入商品促销新增/编辑页面");
		
		StoreGoods storeGoods = new StoreGoods();
		storeGoods = storeGoodsService.one(storeGoodsId);
		if (null == storeGoods || !storeGoods.getStore().equals(getStore(true))) {
			return REDIRECT_INDEX;
		}
		
		if (type >= GoodsPromoteType.values().length) {
			return REDIRECT_INDEX;
		}
		GoodsPromoteType goodsPromoteType = GoodsPromoteType.values()[type];
		
		GoodsPromoteId gpId = new GoodsPromoteId();
		gpId.setStoreGoodsId(storeGoodsId);
		gpId.setType(type);
		GoodsPromote goodsPromote = goodsPromoteService.one(gpId);
		
		GoodsGift goodsGift = new GoodsGift();// goodsGiftForSearch
		GoodsGiftId goodsGiftId = new GoodsGiftId();
		goodsGift.setId(goodsGiftId);
		goodsGiftId.setSellId(storeGoods.getStoreGoodsId());
		
		List<GoodsGift> goodsGiftList = null;
		if (goodsPromoteType == GoodsPromoteType.ONLINE_GIFT) {
			goodsGiftId.setType(GoodsGiftType.ONLINE.ordinal());
			goodsGiftList = goodsGiftService.list(goodsGift);
		} else if (goodsPromoteType == GoodsPromoteType.LOCAL_GIFT) {
			goodsGiftId.setType(GoodsGiftType.LOCAL.ordinal());
			goodsGiftList = goodsGiftService.list(goodsGift);
		}
		
		model.addAttribute("gp", goodsPromote);
		model.addAttribute("sg", storeGoods);
		model.addAttribute("ggList", goodsGiftList);
		model.addAttribute("type", type);
		model.addAttribute("typeText", goodsPromoteType.getText());
		
		return "shop/goodsPromote_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public StatusResult addOrEditDoJson(GoodsPromote goodsPromote, Gifts gifts) {
		debug(log, "保存商品促销信息");
		StatusResult jsonResult = new StatusResult();
		try {
			
			goodsPromoteService.addOrEditByStore(getStore(true), goodsPromote, gifts.getGifts());
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "保存商品促销异常");
			jsonResult.setErrorMessage("保存失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(GoodsPromoteId goodsPromoteId) {
		debug(log, "删除商品促销信息");
		StatusResult jsonResult = new StatusResult();
		try {
			goodsPromoteService.removeByStore(getStore(true), goodsPromoteId);
			
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "删除商品促销信息异常"); 
			jsonResult.setErrorMessage("删除失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/statusDo/json")
	@ResponseBody
	public ObjectResult<?> statusDoJson(GoodsPromoteId goodsPromoteId, GoodsPromoteStatus status) {
		debug(log, "更新商品促销状态");
		ObjectResult<Integer> jsonResult = new ObjectResult<Integer>();
		try {
			goodsPromoteService.editStatusByStore(getStore(true), goodsPromoteId, status);
			jsonResult.setResult(status.ordinal());
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "更新商品促销状态异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delSomeDo/json")
	@ResponseBody
	public StatusResult delSomeDoJson(GoodsPromoteIds goodsPromoteIds) {
		debug(log, "批量删除商品促销");
		StatusResult jsonResult = new StatusResult();
		try {
			goodsPromoteService.removeByStore(getStore(true), goodsPromoteIds.goodsPromoteIds);
			
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "批量删除商品促销异常");
			jsonResult.setErrorMessage("删除失败，请联系管理员");
		}
		return jsonResult;
	}
	
	public static class Gifts {
		private List<GoodsGift> gifts;

		public List<GoodsGift> getGifts() {
			return gifts; 
		}

		public void setGifts(List<GoodsGift> gifts) {
			this.gifts = gifts;
		}
		
	}
	
	public static class GoodsPromoteIds {
		private List<GoodsPromoteId> goodsPromoteIds;

		public List<GoodsPromoteId> getGoodsPromoteIds() {
			return goodsPromoteIds;
		}

		public void setGoodsPromoteIds(List<GoodsPromoteId> goodsPromoteIds) {
			this.goodsPromoteIds = goodsPromoteIds;
		}
		
	}
}
